//
//  DFBaseLineItem.m
//  DFTimelineView
//
//  Created by Allen Zhong on 15/9/27.
//  Copyright (c) 2015年 Datafans, Inc. All rights reserved.
//

#import "DFBaseLineItem.h"

@implementation DFBaseLineItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        //_comments = [NSMutableArray array];
        
        
        
    }
    return self;
}

-(instancetype)initWithDic:(NSDictionary *)dic{

    if (self = [super init]){
        
        _replylist = [NSMutableArray array];
        _commentStrArray = [NSMutableArray array];
        
        [self setValuesForKeysWithDictionary:dic];
        
        [self genLikeAttrString:self];
        [self genCommentAttrString:self];
    }
    
    return self;
}

+(DFBaseLineItem *)dictionaryWithDic:(NSDictionary *)dic{
    
    DFBaseLineItem *obj = [[DFBaseLineItem alloc] initWithDic:dic];
    
    return obj;
}

-(id)valueForUndefinedKey:(NSString *)key{
    
    return nil;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
    
}

-(void)genLikeAttrString:(DFBaseLineItem *)item{
    
    if (item.praiseuser.count == 0)
    {
        return;
    }
    
    if (item.likesStr == nil)
    {
        NSMutableArray *likes = item.praiseuser;
        NSString *result = @"";
        
        for (int i = 0; i<likes.count; i++) {
            
            NSDictionary *dic = likes[i];
            
            if (i == 0)
            {
                result = [NSString stringWithFormat:@"%@",[dic objectForKey:@"username"]];
            }else
            {
                
                result = [NSString stringWithFormat:@"%@, %@", result, [dic objectForKey:@"username"]];
            }
        }
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:result];
            NSUInteger position = 0;
            
        
            for (int i=0; i<likes.count;i++)
            {
                NSDictionary *dic = [likes objectAtIndex:i];
                
                NSString *username = [dic objectForKey:@"username"];
                NSString *userid = [[dic objectForKey:@"userid"] stringValue];
                [attrStr addAttribute:NSLinkAttributeName value:userid range:NSMakeRange(position, username.length)];
                position += username.length+2;
            }
            
            item.likesStr = attrStr;
        
        }
    
}

-(void)genCommentAttrString:(DFBaseLineItem*)item{
    
    NSMutableArray *comments = item.replylist;
    
    [item.commentStrArray removeAllObjects];
    
    for (int i=0; i<comments.count;i++){
        
        NSDictionary *dic = [comments objectAtIndex:i];
        
        NSString *resultStr;
        
        NSInteger replayStr = [[dic objectForKey:@"touserid"] integerValue];
        
        NSString *userName = [dic objectForKey:@"username"];
        NSString *scontent = [dic objectForKey:@"scontent"];
        NSString *userid = [[dic objectForKey:@"userid"] stringValue];
        
        NSString *tousername = [dic objectForKey:@"tousername"];
        NSString *touserid = [dic objectForKey:@"touserid"];
        
        
        
        if (replayStr == 0) {
            
            resultStr = [NSString stringWithFormat:@"%@: %@",[dic objectForKey:@"username"], [dic objectForKey:@"scontent"]];
        }else{
            
            
            resultStr = [NSString stringWithFormat:@"%@回复%@: %@",userName, tousername, scontent];
        }
        
        NSMutableAttributedString *commentStr = [[NSMutableAttributedString alloc]initWithString:resultStr];
        
        if (replayStr == 0) {
        

            [commentStr addAttribute:NSLinkAttributeName value:userid range:NSMakeRange(0, userName.length)];
        }else{
            
            NSUInteger localPos = 0;
            [commentStr addAttribute:NSLinkAttributeName value:userid range:NSMakeRange(localPos, userName.length)];
            localPos += userName.length + 2;
            
            [commentStr addAttribute:NSLinkAttributeName value:touserid range:NSMakeRange(localPos, tousername.length)];
        }
        
        [item.commentStrArray addObject:commentStr];
    }
}


@end

